﻿using AccessibilityTools.DB;
using LogForNetApp;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities.IO.EXCEL;

namespace AttendanceSysytem.Asset
{
    public class AssetManagement
    {
        private Fixed_assets_infoLogic _fixedAssetsInfologic = new Fixed_assets_infoLogic();

        /// <summary>
        /// 刷新主界面
        /// </summary>
        /// <returns></returns>
        public AssetFormDto Refresh()
        {
            AssetFormDto ins = new AssetFormDto();
            ins.AssetClass = _fixedAssetsInfologic.GetAssetClasses();
            ins.AssetName = _fixedAssetsInfologic.GetAssetName();
            ins.AssetUser = _fixedAssetsInfologic.GetAssetUserName();
            ins.AssetBrand = _fixedAssetsInfologic.GetBrandName();
            ins.AssetSupplier = _fixedAssetsInfologic.GetSupplierName();
            ins.AssetPlaceStorage = _fixedAssetsInfologic.GetStoragePlaceName();
            return ins;
        }

        /// <summary>
        /// 生成资产编号
        /// </summary>
        /// <param name="asset_type">资产类别</param>
        /// <param name="department_name">部门</param>
        /// <param name="serialNumber">序列号</param>
        /// <returns></returns>
        public string GenerateAssetNumRule(string asset_type, string department_name)
        {
            string fuzzyStr = $"{asset_type.ToUpper()}-{department_name.ToUpper()}{DateTime.Now.ToString("yyyy")}";
            string serialNumber = _fixedAssetsInfologic.GenerateAssetNum(fuzzyStr);
            string assetNum = $"{fuzzyStr}-{serialNumber.PadLeft(3, '0')}";
            return assetNum;
        }


        /// <summary>
        /// 增加固定资产信息
        /// </summary>
        /// <param name="ins">实例</param>
        /// <returns>返回结果 True-成功</returns>
        public bool AddAssetInfo(Fixed_assets_info ins)
        {
            if (ins != null)
            {
                ins.Creation_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                //查询数据库内是否有重复的编码
                bool isExist = _fixedAssetsInfologic.QueryAssetNumIsExist(ins.Asset_num);
                if (isExist)
                {
                    int res = _fixedAssetsInfologic.AddAssetsInfo(ins);
                    if (res > 0)
                    {
                        return true;
                    }
                    return false;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public string OpenFile()
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Title = "请选择导入文件";
            opf.Filter = "Excel|*.xls;*.xlsx";
            opf.Multiselect = false;
            string filepath = "";
            if (opf.ShowDialog() == DialogResult.OK)
            {
                filepath = opf.FileName;
                LogHelper.WriteInfo($"选择了{opf.FileName}");
                return filepath;
            }
            return "";
        }

        public List<Fixed_assets_info> TransferData(string filepath)
        {
            NpoiHelper excelNpoi = new NpoiHelper(filepath, 0);
            LastPosition lstpos = excelNpoi.GetLastPosition();
            string[,] details = excelNpoi.GetSheetData(lstpos.lastRow, lstpos.lastCol);
            /*
             顺序：
            0-序号 1-资产名称 2-类别 3-资产名称 4-规格型号 5-品牌 6-供应商 7-数量 8-单位 9-单价 10-税额 
            11-合计 12-购置日期 13-使用人 14-存放地点 15-在使用 16-未使用 
            17-报废 18-清查结果 19-备注 20-绿盾 22-签字确认
             */
            List<Fixed_assets_info> lstAssets = new List<Fixed_assets_info>();

            for (int i = 6; i < details.GetLength(0); i++)
            {
                Fixed_assets_info ins = new Fixed_assets_info()
                {
                    Asset_num = details[i, 1],
                    Asset_class = details[i, 2],
                    Asset_name = details[i, 3],
                    Asset_type = details[i, 4],
                    Asset_brand = details[i, 5],
                    Asset_supplier = details[i, 6],
                    Asset_count = details[i, 7],
                    Asset_unit = details[i, 8],
                    Unit_price = double.TryParse(details[i, 9], out double resunit) ? Convert.ToDouble(details[i, 9]) : 0,
                    Price_unit_name = "元",
                    Used_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    Creation_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    Price_tax = details[i, 10],
                    Total_price = double.TryParse(details[i, 11], out double res) ? Convert.ToDouble(details[i, 11]) : 0,
                    Buy_time = details[i, 12],
                    User_name = details[i, 13],
                    Place_storage = details[i, 14],
                    Is_used = !details[i, 15].IsNullOrEmpty() ? 1 : 0,
                    IsDeleted = !details[i, 17].IsNullOrEmpty() ? 1 : 0,
                    Inventory_result = details[i, 18],
                    Asset_notes = details[i, 19],
                    Green_shield = details[i, 20].IsNullOrEmpty() ? 0 : 1,
                    Is_signed = details[i, 22].IsNullOrEmpty() ? 0 : 1
                };
                lstAssets.Add(ins);
            }
            _fixedAssetsInfologic.AddAssetsInfo(lstAssets);
            return lstAssets;

        }

    }

    public class AssetFormDto
    {
        /// <summary>
        /// 资产类别
        /// </summary>
        public List<string> AssetClass { get; set; }
        /// <summary>
        /// 资产名称
        /// </summary>
        public List<string> AssetName { get; set; }
        /// <summary>
        /// 品牌
        /// </summary>
        public List<string> AssetBrand { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<string> AssetSupplier { get; set; }
        /// <summary>
        /// 使用人
        /// </summary>
        public List<string> AssetUser { get; set; }
        /// <summary>
        /// 存放地点
        /// </summary>
        public List<string> AssetPlaceStorage { get; set; }
    }

}
